package ch.fhnw.qualiscan.application.modals;

import ch.fhnw.qualiscan.Config;
import ch.fhnw.qualiscan.application.models.SurveyModel;
import ch.fhnw.qualiscan.core.Modal;
import ch.fhnw.qualiscan.core.db.helpers.participantsTableArgs;

import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;

import eu.livotov.tpt.i18n.TM;
/**
 * Modal Box to delete a user from a survey.
 * @author Kevin Mettenberger
 *
 */
@SuppressWarnings("serial")
public class DeleteParticipantFromSurvey extends Modal {
	
	//Table Container Component
	public participantsTableArgs pta;
	private Button confirmButton;
	private Button abortButton;
	private SurveyModel surveyModel;
	private BeanItemContainer<participantsTableArgs> tableContainer;
	
	public DeleteParticipantFromSurvey(participantsTableArgs pta, BeanItemContainer<participantsTableArgs> tableContainer) {
		setCaption(TM.get("deleteparticipant.title"));
		this.pta = pta;
		this.tableContainer = tableContainer;
		surveyModel = new SurveyModel();
		buildLayout();
	}
	
	public void buildLayout() {
		String name;
		if(pta.getFirstname().equals("")) {
			name = pta.getEmail();
		} else {
			name = pta.getFirstname() + " " + pta.getLastname();
		}
		
		Label description = textHelper.p(TM.get("deleteparticipant.description",  name));
		
		HorizontalLayout buttonBar = new HorizontalLayout();
		buttonBar.setSpacing(true);
		buttonBar.setMargin(true, false, true, false);
		
		confirmButton = new Button(TM.get("action.confirm"), new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				surveyModel.deleteParticipant(pta.getSur());
				tableContainer.removeItem(pta);
				DeleteParticipantFromSurvey dpfs = (DeleteParticipantFromSurvey) event.getButton().getData();
				dpfs.close();
			}
		});
		confirmButton.setIcon(Config.ICON_ACCEPT);
		confirmButton.setData(this);
		
		abortButton = new Button(TM.get("action.cancel"), new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				DeleteParticipantFromSurvey dpfs = (DeleteParticipantFromSurvey) event.getButton().getData();
				dpfs.close();
			}
		});
		abortButton.setIcon(Config.ICON_CANCEL);
		abortButton.setData(this);
		
		buttonBar.addComponent(confirmButton);
		buttonBar.addComponent(abortButton);
		
		this.addComponent(description);
		this.addComponent(buttonBar);
		
	}
}
